import { post, get } from '../utils/request';
import type { PagedResponseData } from '../utils/request';

// 订单商品接口
export interface OrderProduct {
  name: string;
  quantity: number;
  price: number;
}

// 创建订单请求接口
export interface CreateOrderRequest {
  customerName: string;
  customerPhone: string;
  paymentMethod: string;
  products: OrderProduct[];
}

// 订单接口
export interface Order {
  id: number;
  orderNo: string;
  customerName: string;
  customerPhone: string;
  totalAmount: number;
  status: string;
  paymentMethod: string;
  createTime: string;
  products: OrderProduct[];
}

// 创建订单
export const createOrder = (data: CreateOrderRequest): Promise<{
  isSuccess: boolean;
  data: Order;
  message: string;
  code: number;
  timestamp: number;
}> => {
  return post('/api/v1/orders', data);
};

// 获取订单列表
export const getOrders = (page: number = 1, pageSize: number = 10, status?: string): Promise<PagedResponseData<Order>> => {
  const params = new URLSearchParams({
    page: page.toString(),
    pageSize: pageSize.toString()
  });
  
  if (status) {
    params.append('status', status);
  }
  
  return get(`/api/v1/orders?${params.toString()}`) as Promise<PagedResponseData<Order>>;
};

// 获取订单详情
export const getOrderById = (id: number): Promise<{
  isSuccess: boolean;
  data: Order;
  message: string;
  code: number;
  timestamp: number;
}> => {
  return get(`/api/v1/orders/${id}`);
};

// 更新订单状态
export const updateOrderStatus = (id: number, status: string): Promise<{
  isSuccess: boolean;
  data: Order;
  message: string;
  code: number;
  timestamp: number;
}> => {
  return post(`/api/v1/orders/${id}/status`, { status });
};
